package xin.tianhui.cloud.weblog.web;




import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import lombok.extern.slf4j.Slf4j;
import xin.tianhui.cloud.weblog.common.BaseController;
import xin.tianhui.cloud.weblog.constant.Constants;
import xin.tianhui.cloud.weblog.constant.RequestUriPath;
import xin.tianhui.cloud.weblog.manager.IArticleMng;
import xin.tianhui.cloud.weblog.manager.IChannelMng;
import xin.tianhui.cloud.weblog.model.dto.ArticleResultDTO;
import xin.tianhui.cloud.weblog.model.dto.ChannelResultDTO;

/**
 * @author junchen
 *
 */
@Controller
@Slf4j
public class DynamicController extends BaseController{
	
	/**
	 * 栏目
	 * <span>about.html || a.html</span>
	 * @param path
	 * @return
	 */
	@RequestMapping(value= {"{path}"+Constants.REQUEST_PATH_SUFFIX},method=RequestMethod.GET)
	public String channel(@PathVariable(name="path") String path,@RequestParam(defaultValue="1")Long page,ModelMap model,HttpServletResponse response) {
		ChannelResultDTO dto = channelMng.getChannelByPath(path);
		if(dto!=null && dto.getModel() != null && StringUtils.isNotBlank(dto.getModel().getChannelPrefix())) {
			model.addAttribute("channel", dto);
			model.addAttribute("currentPage",page);
			return getReturnPath(Constants.CHANNEL_PREFIX,dto.getModel().getChannelPrefix());
		}
		response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		return getReturnPath(Constants.COMMON_PREFIX,Constants.PAGE_NOT_FOUND);
	}
	
	/**
	 * 文章
	 * <span>about/1.html</span>
	 * @param prefix
	 * @param suffix
	 * @return
	 */
	@RequestMapping(value= {"{prefix}/{suffix}"+Constants.REQUEST_PATH_SUFFIX},method=RequestMethod.GET)
	public String article(@PathVariable(name="prefix") String prefix,@PathVariable(name="suffix")String suffix,ModelMap model,HttpServletResponse response) {
		ChannelResultDTO channel = null;
		if(StringUtils.isNotBlank(prefix)) {
			//获取栏目
			channel = channelMng.getChannelByPath(prefix);
		}
		//没有找到
		if(channel == null ) {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
			return getReturnPath(Constants.COMMON_PREFIX,Constants.PAGE_NOT_FOUND);
		}
		if(StringUtils.isNotBlank(suffix)) {
			//获取文章
			ArticleResultDTO article = articleMng.getArticle(Integer.parseInt(suffix));
			if(channel != null && StringUtils.isNotBlank(channel.getModel().getArticlePrefix()) && article != null) {
				article.setChannel(channel);
				model.addAttribute("article",article);
				return getReturnPath(Constants.ARTICLE_PREFIX,channel.getModel().getArticlePrefix());
			}
		}
		response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		return getReturnPath(Constants.COMMON_PREFIX,Constants.PAGE_NOT_FOUND);
	}
	
	/**
	 * <p>Title: classList</p>
	 * <p>Description: 文章类别</p>
	 * @param suffix
	 * @param model
	 * @param response
	 * @return
	 * @author junchen homeanter@163.com
	 * @date 2018-04-07 16:38:52
	 */
	@RequestMapping(value= {RequestUriPath.ARTICLE_TYPE_PREFIX+"/{suffix}"+Constants.REQUEST_PATH_SUFFIX},method=RequestMethod.GET)
	public String classList(@PathVariable(name="suffix")String suffix,ModelMap model,HttpServletResponse response) {
		return "index";
	}
	
	/**
	 * 网站首页
	 * @return
	 */
	@RequestMapping(value= {"","/","index"+Constants.REQUEST_PATH_SUFFIX},method=RequestMethod.GET)
	public String index(ModelMap model) {
		return "index";
	}
	
	@Autowired
	private IChannelMng channelMng;
	
	@Autowired
	private IArticleMng articleMng;
}
